<?php

use Biz\service\wiki\WikiMiddle;

/**
 * Description: 找好物中间页
 * Created by PhpStorm.
 * User: congyukun
 * Date: 2021/4/30
 * Time: 14:31
 */
class FindWikiMiddleListAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;
    const IS_LOG_EXPOSE = 1;

    public function main()
    {
        try {
            $list = [];
            //分类di
            $classify_id = $this->getRequest()->getQuery('classify_id', 0);
            $classify_name = $this->getRequest()->getQuery('classify_name', '');
            $page = $this->getRequest()->getQuery('page', 1);
            $per_page = $this->getRequest()->getQuery('per_page', 6);
            $uid = $this->uid;
            if (!$classify_id) {
                throw new \UnexpectedValueException('参数错误');
            }

            //根据分类信息 查找所有二级分类
            $list['name'] = $classify_name . '合集';
            //偏移量，当前页-1乘以每页显示条数
            $start = ($page - 1) * $per_page;

            $list['per_page'] = $per_page;
            $list['current_page'] = $page;

            //取所有wiki 根据热多排序
            $wiki_ids = \Biz\service\wiki\FindWIkiService::getWikiIdsByClassifyId($classify_id, $start, $per_page);
            //每页的wiki_id集合
            $data = [];
            if ($wiki_ids) {
                $params = [
                    'wiki_ids' => $wiki_ids,
                    'uid' => $uid
                ];
                $data = WikiMiddle::getMiddleWikiList($params);
            }


            $list['list'] = array_values($data);
            $list['is_over'] = $wiki_ids ? 1 : 0;
            $this->setSuccess($list);

        } catch (UnexpectedValueException $e) {
            return $this->setError(0, $e->getMessage());
        } catch (Exception $e) {
            \Doraemon\tools\Log::error('找好物中间页-error:' . $e->getMessage(), $this->getRequest()->getPost());
            return $this->setSuccess(['msg' => '系统错误: ' . $e->getMessage()], 0);
        }
    }
}